/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.statusChanges;

import java.awt.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.GrantRequest.GrantRequest;
import researchgrants.parts.db.Db;


/**
 * A single status change. Each subclass needs to define it own behavior.
 * However, each subclass also needs to add code here also.
 * @author DOStudent1
 */
public abstract class GrantRequestStatusChange {

    // allows each different status change to return its own view
    public abstract GrantRequestStatusChangeView getView(Window parent);

    // identifies the status change
    public abstract String getStatusChangeTitle();
    public abstract String getStatusChangeCode();
    
    // the id of the status change - all different types have an id derived from here
    private int statusChangeId;

    protected GrantRequestStatusChange(int statusChangeId) {
        this.statusChangeId = statusChangeId;
    }

    public int getStatusChangeId() {
        return(statusChangeId);
    }

    // creates the common part regarding different status changes for a new status change
    // after calling this method, each caller needs to save its own independent data
    static int createNewStatusChangeForGrantRequest(GrantRequest grantRequest, String type) {
        int newStatusChangeId=-1;
        try {
            Connection conn = Db.openDbConnection();
            PreparedStatement statement = Db.createPreparedStatement(conn, "INSERT INTO tblGrantRequestsStatusChanges (GrantRequestID, StatusChangeType) VALUES (?,?)");
            statement.setInt(1, grantRequest.getId());
            statement.setString(2, type);
            statement.executeUpdate();
            newStatusChangeId=Db.getLastIdentity();
            statement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(GrantRequestStatusChange.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return(newStatusChangeId);
    }
    
    
    // This deletion may be overriden by child implementations. It should be overriden if additional steps needs to be carried to delete their data
    // However, right now, no additional steps need to be taken.
    public void delete() {
        try {
            Connection conn = Db.openDbConnection();
            // moves everything to an archive of GrantRequestStatusChanges
            PreparedStatement statement = Db.createPreparedStatement(conn, "INSERT INTO tblGrantRequestsDeletedStatusChanges (GrantRequestID, GrantRequestStatusChangeID, InsertionDate, StatusChangeType) SELECT GrantRequestID, GrantRequestStatusChangeID, InsertionDate, StatusChangeType FROM tblGrantRequestsStatusChanges WHERE tblGrantRequestsStatusChanges.GrantRequestStatusChangeID=?");
            statement.setInt(1, statusChangeId);
            statement.executeUpdate();
            statement.close();
            statement = Db.createPreparedStatement(conn, "DELETE FROM tblGrantRequestsStatusChanges WHERE GrantRequestStatusChangeID=?");
            statement.setInt(1, statusChangeId);
            statement.executeUpdate();
            statement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(GrantRequestStatusChange.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    // returns the title of a given last GrantRequestStatusChange
    public static String getStatusChangeTitle(GrantRequestStatusChange lastGrantRequestStatusChange) {
        if (lastGrantRequestStatusChange==null) {
            // when the last GrantRequestStatusChange is null, it means that it has no status changes so it remains unsubmitted
            return("Unsubmitted");
        } else {
            // retreives the title of the status change
            return(lastGrantRequestStatusChange.getStatusChangeTitle());
        }
    }

    
}